package com.csii.mytest.service;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Binder;
import android.os.IBinder;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.util.Log;

import com.csii.mytest.MainActivity;
import com.csii.mytest.R;

import java.util.concurrent.TimeUnit;

/**
 * Created by 顾博君 on 2017/3/2.
 */

public class MyService extends Service {
    public static final String TAG = "MyService";
    private MyBinder mBinder = new MyBinder();
    private int count = 0;

    @Override
    public void onCreate() {
        super.onCreate();
        showDefaultNotification();
        Log.d(TAG, "onCreate() executed");
    }

    // 默认显示的的Notification
    private void showDefaultNotification() {
        Notification.Builder builder = new Notification.Builder(
                MyService.this);
        //将该通知显示为默认View
        Intent notificationIntent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
                notificationIntent, 0);
        builder.setContentIntent(pendingIntent);
        builder.setSmallIcon(R.mipmap.ic_launcher);// 设置图标
        builder.setWhen(System.currentTimeMillis());// 设置通知来到的时间
        // builder.setAutoCancel(true);
        builder.setContentTitle("标题");// 设置通知的标题
        builder.setContentText("内容");// 设置通知的内容
        builder.setTicker("状态栏上显示");// 状态栏上显示
        builder.setAutoCancel(true);
        /*
             * // 设置声音(手机中的音频文件) String path =
             * Environment.getExternalStorageDirectory() .getAbsolutePath() +
             * "/Music/a.mp3"; File file = new File(path);
             * builder.setSound(Uri.fromFile(file));
             */

        // 获取Android多媒体库内的铃声
        builder.setSound(Uri.withAppendedPath(
                MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "5"));

        // builder.setVibrate(new long[]{2000,1000,4000}); //需要真机测试
        Notification notification = null;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
            notification = builder.build();
        }
        // 设置setLatestEventInfo方法,如果不设置会App报错异常
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        //注册此通知
        // 如果该NOTIFICATION_ID的通知已存在，会显示最新通知的相关信息 ，比如tickerText 等
        mNotificationManager.notify(0, notification);

    }

    private void removeNotification() {
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        // 取消的只是当前Context的Notification
        mNotificationManager.cancel(0);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG, "onStartCommand() executed");
        new Thread(new Runnable() {
            @Override
            public void run() {
                // 开始执行后台任务
                while (count < 100) {
                    count++;
                    Log.v(TAG, "count:" + count);
                    try {
                        TimeUnit.MILLISECONDS.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        count = 100;
        removeNotification();
        super.onDestroy();
        Log.d(TAG, "onDestroy() executed");
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    public class MyBinder extends Binder {
        private int progress = 0;

        public void startDownload() {
            Log.d("TAG", "startDownload() executed");
            // 执行具体的下载任务
            new Thread(new Runnable() {
                @Override
                public void run() {
                    // 执行具体的下载任务
                    while (progress < 100) {
                        progress++;
                        Log.v(TAG, "progress:" + progress);
                        try {
                            TimeUnit.MILLISECONDS.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }).start();
        }
    }
}
